// manager/pages/todotask/index.js
import { request } from '../../../utils/request';
import util from '../../../common/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activeIndex: 0,
        tabList: [
            {
				id: 1,
                text: "待处理"
            },
            {
				id: 2,
                text: "已提交"
            },
            {
				id: 3,
                text: "已办结"
            }
		],
        sts: 0,
        params: {
            search: "",
        },
        scroller: {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: [
                {
                    waitTime: 20, 
                    flowName: "业务申请", 
                    stepName: "业务申请", 
                    cname: "热尔", 
                    createTime: "2024-20-07 10:35:00", 
                    cardNum: 110101200307288904, 
                    phoneNum: 167777775555, 
                }
            ]
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.getDictionaryInfo();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
		
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 页面加载时执行一次加载
        this.resetScroller()
        this.getData()
    },

    resetScroller(){
        let scroller = {
			pageNum: 0,
			pageSize: 10,
			totalPage: 10,
			isNoneLoading: true,
			refreshColor: '#000000',
			refreshDiy: false,
			isloaded: false,
			list: []
		}
		this.setData({
			scroller
		})
    },

    tabChange(e){
        let { index } = e.detail;
        this.setData({
            activeIndex: index
        })
		this.resetScroller()
		this.getData()
    },
    /**
     * 获取字典
     */
    getDictionaryInfo() {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
				dictType: "ecard_cust_type"
			}
        }).then(res => {
            if(res.code == 0){
                let typeList = [];
                // 是否显示个人
                if(this.data.isPerson){
                    typeList = res.data;
                }else{
                    res.data.forEach(item => {
                        if(item.dictValue != 1){
                            typeList.push(item)
                        }
                    })
                }
                // 默认商户
                let typeIndex = typeList.findIndex(item => item.dictValue == 2)
                this.setData({
                    typeList,
                    typeIndex
                })
            }
        }).catch(error => {
            console.log(error)
        })
    },
    /**
     * 输入框输入时
     */
    input(e){
        let key = e.target.dataset.key;
        this.setData({
            ['params.'+key]: e.detail.value,
            // isFilter: !!e.detail.value,
        })
    },
    // 搜索
    searchConfirm(e){
        console.log(e.detail.value)
        this.setData({
            'params.search': e.detail.value
        })
        // 页面加载时执行一次加载
		this.resetScroller()
		this.getData()
    },
    searchBtn(e){
        this.setData({
            'params.merchantType': this.data.typeList[this.data.typeIndex].dictValue
        })
        this.resetScroller()
		this.getData()
    },
    /**
     * 获取网格任务列表
     */
    getData() {
        let { activeIndex } = this.data;
        switch(activeIndex){
            case 0:
                this.getWaitdoList()
                break;
            case 1: 
                this.hasSubmitList()
                break;
            case 2: 
                this.closedTaskList()
                break;
            default: 
                break;
        }
    },

    /** 跳转删除流程 */
    toDeleteprocess(e) {
        let { item } = e.currentTarget.dataset;
        wx.navigateTo({
            url: `/manager/pages/deleteprocess/index?versionId=${item.versionId}&instanceId=${item.instanceId}&stepId=${item.stepId}&taskId=${item.taskId}&keyInfo=${encodeURIComponent(item.keyInfo)}`,
        })
    },

    /**
     * 获取待处理任务列表
     */
    getWaitdoList: function () {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/businessFlow/getWaitdoList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
                ...this.data.params
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.createTime = item.createTime?util.formatTime(new Date(item.createTime)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                console.log(res.data,scroller.list)
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            console.log(error)
        })	
    },
    /**
     * 获取已提交任务列表
     */
    hasSubmitList: function () {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/businessFlow/hasSubmitList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
                ...this.data.params
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.createTime = item.createTime?util.formatTime(new Date(item.createTime)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                console.log(res.data,scroller.list)
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            console.log(error)
        })	
    },
    /**
     * 获取已办结任务列表
     */
    closedTaskList: function () {
		let { scroller } = this.data;
        scroller.pageNum = scroller.pageNum + 1;
        if(scroller.pageNum>scroller.totalPage){
            return
        }
        request({
            url: `/businessFlow/closedTaskList`,
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                pageNum: this.data.scroller.pageNum,
                pageSize: this.data.scroller.pageSize,
                ...this.data.params
            }
        }).then(res => {
            if(res.code == 0){
                scroller.totalPage = Math.ceil(res.total/scroller.pageSize);
                if(scroller.pageNum>=scroller.totalPage){
                    scroller.isNoneLoading = false;
                }
                if(res.data.length>0){
                    res.data.map(item => {
                        item.createTime = item.createTime?util.formatTime(new Date(item.createTime)):"";
                        return item
                    })
                }
                if(scroller.pageNum == 1){
                    scroller.list = res.data;
                }else{
                    scroller.list = scroller.list.concat(res.data);
                }
                console.log(res.data,scroller.list)
                this.setData({
                    scroller
                })
            }
        }).catch(error => {
            console.log(error)
        })	
    },
    
	refresh: function () {
        this.resetScroller()
		this.getData()
    },
    
	loadMore: function () {
		this.getData()
    },

    /** 跳转审批记录 */
    toApprovalrecord(e) {
        let { item } = e.currentTarget.dataset;
        console.log(item)
        wx.navigateTo({
          url: `/manager/pages/approvalrecord/index?instanceId=${item.instanceId}`,
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    }
})